/*
 * Copyright 2008 the original author Ang Gao.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * TomcatServer.java 2008-11-25
 */
package ie.ucc.cs.anggao.finalproject.client.support.searchengine;

import ie.ucc.cs.anggao.finalproject.client.model.Query;
import ie.ucc.cs.anggao.finalproject.client.model.QueryEssay;
import ie.ucc.cs.anggao.finalproject.client.model.SearchEngineResult;
import ie.ucc.cs.anggao.finalproject.client.service.IRService;
import ie.ucc.cs.anggao.finalproject.client.service.IRServiceAsync;
import ie.ucc.cs.anggao.finalproject.client.ui.event.QueryEvent;
import ie.ucc.cs.anggao.finalproject.client.ui.mainpanel.infopane.InfoPane;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * @author GaoAng
 * 
 */
public class TomcatServer {

	private IRServiceAsync service;

	private Query query;
	
	private ArrayList<SearchEngineResult> searchengineresult;

	private InfoPane view;	
	
	private AggregatedSearcher aggregatedsearcher;
	
	public TomcatServer(InfoPane view, AggregatedSearcher aggregatedsearcher) {
		// super(view, new SearchEngine("Google Search",
		// "http://www.google.com"));
		service = IRService.IR.getInstance();
		query = new Query();
		this.view = view;
		this.aggregatedsearcher = aggregatedsearcher;
	}

	public void doIR(QueryEvent q) {
		/*
		 * In the future add flag to control get Aggregated Result or only Intelligent Query
		 */
		service.getSEresult(new Query(q.getQuery()), new TomcatCallBack_Aggregated());
		
		service.getSEquery(new QueryEssay(q), new TomcatCallBack());

		// getView().clearResults();
	}

	private class TomcatCallBack implements AsyncCallback<Query> {

		public void onFailure(Throwable caught) {
			query = null;
		}

		public void onSuccess(Query result) {
//			System.out.println(result.getQuery());
			query = result;
			
			view.queryEvent(query);
		}

	}

	private class TomcatCallBack_Aggregated implements
			AsyncCallback<List<SearchEngineResult>> {

		public void onFailure(Throwable caught) {

		}

		public void onSuccess(List<SearchEngineResult> results) {
			searchengineresult = (ArrayList<SearchEngineResult>) results;
			
			aggregatedsearcher.addResultsToView(searchengineresult);
			
//			for (SearchEngineResult SEresult : searchengineresult) {
//
//				System.out.println("Result-------------"+SEresult.getTitle());
//
//			}
			
			
		}
	}

	public Query getQuery() {
		return query;
	}
	
	public ArrayList<SearchEngineResult> getResults(){
		return searchengineresult;
	}

}
